#include "EmployeeManagementSystem.h"
StaffNode * StaffNode::p = nullptr;
#define FILENAME "/Users/hefeiyu/Desktop/learnC++/learnC++/employee.txt" // 可自定义文件路径

// 展示目录
void displayMenu() {
    cout << "欢迎进入职工管理系统！" << endl;
    cout << "-----------------------" << endl;
    cout << "1 退出管理系统" << endl;
    cout << "2 批量增加职工信息" << endl;
    cout << "3 显示所有职工信息" << endl;
    cout << "4 删除离职员工" << endl;
    cout << "5 修改职工信息" << endl;
    cout << "6 按编号查找员工信息" << endl;
    cout << "7 按名字查找员工信息" << endl;
    cout << "8 按照编号排序" << endl;
    cout << "9 清空所有文档" << endl;
    cout << "请输入：" << endl;
}

// 读取文件数据
void readFile(StaffNode * &p) {
    ifstream ifs;
    ifs.open(FILENAME,ios::in);
    if(!ifs.is_open()) {
        cout << "文件不存在！" << endl;
        return;
    }
    string line;
    int id = 0,deid = 0;string name,position = "无";
    bool ishead = 1;
    for(StaffNode *i = p;getline(ifs,line);) {
        // 先检索部门编号，再检索编号
        if(line.find("部门编号:") != string::npos) {
            deid = stoi(line.substr(line.find(":") + 1));
        }
        else if(line.find("姓名:") != string::npos) {
            name = line.substr(line.find(":") + 1);
        }
        else if(line.find("编号:") != string::npos) {
            id = stoi(line.substr(line.find(":") + 1));
        }
        else if(line.find("岗位:") != string::npos) {
            position = line.substr(line.find(":") + 1);
        }
        if(position == "普通职工") {
            if(ishead) {
                i = new StaffNode;
                i->next = nullptr;
                i->m_Staff = new Staff(id,name,deid);
                p = i;
                ishead = 0;
            }
            else {
                StaffNode *j = new StaffNode;
                j->next = nullptr;
                j->m_Staff = new Staff(id,name,deid);
                i->next = j;
                i = j;
            }
        }
        else if(position == "经理") {
            if(ishead) {
                i = new StaffNode;
                i->next = nullptr;
                i->m_Staff = new Manager(id,name,deid);
                p = i;
                ishead = 0;
            }
            else {
                StaffNode *j = new StaffNode;
                j->next = nullptr;
                j->m_Staff = new Manager(id,name,deid);
                i->next = j;
                i = j;
            }
        }
        else if(position == "老板") {
            if(ishead) {
                i = new StaffNode;
                i->next = nullptr;
                i->m_Staff = new Boss(id,name,deid);
                p = i;
                ishead = 0;
            }
            else {
                StaffNode *j = new StaffNode;
                j->next = nullptr;
                j->m_Staff = new Boss(id,name,deid);
                i->next = j;
                i = j;
            }
        }
        position = "无";
    }
    ifs.close();
}

// 重载<<运算符
ofstream & operator<<(ofstream &ofs,Staff * s) {
    ofs << "编号:" << s->m_Id << endl;
    ofs << "姓名:" << s->m_Name << endl;
    ofs << "部门编号:" << s->m_Deid << endl;
    ofs << "岗位:" << s->m_Position << endl;
    ofs << endl;
    return ofs;
}

// 显示所有职工信息
void displayStaff(StaffNode * &p) {
    if(p == nullptr) {
        cout << "尚没有职员" << endl;
        return;
    }
    cout << "-------------------------" << endl;
    for(StaffNode * i = p;i != nullptr;i = i->next) {
        i->m_Staff->displayAll();
        cout << endl;
    }
}

// 添加一个职工
void addaStaff(StaffNode * &i) {
    int id,deid,select;
        string name;
        cout << "编号？" << endl;
        cin >> id;
        cout << "姓名？" << endl;
        cin >> name;
        cout << "部门编号？" << endl;
        cin >> deid;
        cout << "是老板输入1，经理输入2，普通员工输入3:" << endl;
        cin >> select;
        switch(select) {
            case 1:
            i->m_Staff = new Boss(id,name,deid);
            break;
            case 2:
            i->m_Staff = new Manager(id,name,deid);
            break;
            case 3:
            i->m_Staff = new Staff(id,name,deid);
            break;
            default:
            cout << "无效的输入" << endl;
        }
        i->next = nullptr;
}

// 更新文件
void updateFile(StaffNode * &p) {
    ofstream ofs;
    ofs.open
    (FILENAME,ios::out);
    if(!ofs.is_open()) {
        cout << "打开文件失败" << endl;
        return;
    }
    for(StaffNode *i = p;i != nullptr;i = i->next) {
        ofs << i->m_Staff;
    }
    ofs.close();
}

// 批量添加职工信息，并录入文件中
void addStaffs(StaffNode * &p) {
    int num = 0;
    cout << "请输入批量添加的人数：" << endl;
    cin >> num;
    if(num <= 0) {
        cout << "无效的数字" << endl;
        return;
    }
    int c = 1;
    while(num--) {
        cout << "-------------------------" << endl;
        cout << "添加的第" << c << "个职员" << endl;
        c++;
        StaffNode * i = p;
        if(i == nullptr) {
            // 链表为空时
            i = new StaffNode;
            addaStaff(i);
            p = i;
        }
        else {
            // 链表不为空时
            while(i->next != nullptr) {
                i = i->next;
            }
            i->next = new StaffNode;
            addaStaff(i->next);
        }
    }
    updateFile(p);
    cout << "添加成功！" << endl;
}

// 按照编号删除员工信息
void deleteStaff(StaffNode * &p) {
    if(p == nullptr) {
        cout << "尚没有职员" << endl;
        return;
    }
    int id;
    cout << "请输入要删除员工的编号：" << endl;
    cin >> id;
    StaffNode *i = nullptr;
    StaffNode *j = nullptr;
    for(i = p;i != nullptr;i = i->next) {
        if(i->m_Staff->m_Id == id) {
            if(i == p) {
                p = i->next;
                delete i;
                updateFile(p);
                cout << "删除成功！" << endl;
                return;
            }
            j->next = i->next;
            delete i;
            updateFile(p);
            cout << "删除成功！" << endl;
            return;
        }
        j = i;
    }
    cout << "未找到指定编号的成员" << endl;
}

// 按照编号查找员工信息
void searchStaff1(StaffNode * &p) {
    if(p == nullptr) {
        cout << "尚没有职员" << endl;
        return;
    }
    int id;
    cout << "请输入要查找员工的编号：" << endl;
    cin >> id;
    StaffNode *i;
    for(i = p;i != nullptr;i = i->next) {
        if(i->m_Staff->m_Id == id) {
            i->m_Staff->displayAll();
            return;
        }
    }
    cout << "未找到指定编号的成员" << endl;
}

// 按照名字查找员工信息
void searchStaff2(StaffNode * &p) {
    if(p == nullptr) {
        cout << "尚没有职员" << endl;
        return;
    }
    string name;
    cout << "请输入要查找员工的名字：" << endl;
    cin >> name;
    StaffNode *i;
    for(i = p;i != nullptr;i = i->next) {
        if(i->m_Staff->m_Name == name) {
            i->m_Staff->displayAll();
            return;
        }
    }
    cout << "未找到指定编号的成员" << endl;
}

// 按照编号修改员工信息
void modifyStaff(StaffNode * &p) {
    if(p == nullptr) {
        cout << "尚没有职员" << endl;
        return;
    }
    int id;
    cout << "请输入要修改信息的员工的编号：" << endl;
    cin >> id;
    StaffNode *i;
    for(i = p;i != nullptr;i = i->next) {
        if(i->m_Staff->m_Id == id) {
            i->m_Staff->displayAll();
            cout << "请输入修改后的值：" << endl;
            cout << "-------------------" << endl;
            int id,deid;
            string name;
            cout << "编号？" << endl;
            cin >> id;i->m_Staff->m_Id = id;
            cout << "姓名？" << endl;
            cin >> name;i->m_Staff->m_Name = name;
            cout << "部门编号？" << endl;
            cin >> deid;i->m_Staff->m_Deid = deid;
            updateFile(p);
            cout << "修改成功！" << endl;
            return;
        }
    }
    cout << "未找到指定编号的成员" << endl;
}

// 清空文件所有信息
void deleteAll(StaffNode * &p) {
    cout << "请确定您的操作，是输入1，否输入2，请输入：" << endl;
    int a;
    cin >> a;
    if(a == 2)
    return;
    StaffNode *i = p;
    while(i != nullptr) {
        StaffNode *j = i;
        i = i->next;
        delete j;
    }
    p = nullptr;
    updateFile(p);
    cout << "清空成功！" << endl;
}

// 按编号指定方式排序
void sortStaff(StaffNode * &p) {
    if(p == nullptr) {
        cout << "尚没有职员" << endl;
        return;
    }
    cout << "请输入排序方式，升序输入1，降序输入2，请输入：" << endl;
    int b;
    cin >> b;
    if(b == 1) {
        // 升序
        StaffNode *temp = p;
        while(temp) {
            StaffNode *min = temp;
            StaffNode *r = min->next;
            while(r) {
                if(min->m_Staff->m_Id > r->m_Staff->m_Id) {
                    min = r;
                }
                r = r->next;
            }
            swap(temp->m_Staff,min->m_Staff);
            temp = temp->next;
        }
        updateFile(p);
        cout << "排序成功！" << endl;
    }
    else if(b == 2) {
        // 降序
        StaffNode *temp = p;
        while(temp) {
            StaffNode *max = temp;
            StaffNode *r = max->next;
            while(r) {
                if(max->m_Staff->m_Id < r->m_Staff->m_Id) {
                    max = r;
                }
                r = r->next;
            }
            swap(temp->m_Staff,max->m_Staff);
            temp = temp->next;
        }
        updateFile(p);
        cout << "排序成功！" << endl;
    }
    else {
        cout << "无效输入" << endl;
    }
}